const controllers = require('../controllers')
const KoaRouter = require('koa-router')
const router = new KoaRouter()
const multer = require('koa-multer')
const path = require('path')
const fs = require('fs')

const storage = multer.diskStorage({
  destination: function(req, file, cb) {
    const dir = path.join(__dirname, `../webroot/files/${req.url.split('&id=')[1]}`)
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    cb(null, dir)
  },
  filename: function(req, file, cb) {
  	const filename = `${Date.now()}.jpg`
  	req.body.filename = filename
    cb(null, filename);
  }
})

const upload = multer({ storage: storage });

router.post('/login', controllers.login)

router.post('/modpwd', controllers.modpwd)

router.post('/getnews', controllers.getnews)

router.get('/getnew', controllers.getnews)

router.post('/version', controllers.version)

router.post('/getplace', controllers.getplace)

router.post('/village', controllers.village)

router.post('/house', controllers.house)

router.post('/upload', upload.single('file'), controllers.upload)

router.post('/selectview', controllers.selectview)

router.post('/getdata', controllers.getdata)

router.post('/getphoto', controllers.getphoto)

router.post('/practicable', controllers.practicable)

router.post('/checklist', controllers.checklist)

router.post('/backlogview', controllers.backlogview)

module.exports = router